package com.maltys.dp;

/**
 * @author malty
 */
public class UncrossedLines {
    public static void main(String[] args) {
        UncrossedLines uncrossedLines = new UncrossedLines();
        int maxUncrossedLines = uncrossedLines.maxUncrossedLines(new int[]{1, 3, 7, 1, 7, 5}, new int[]{1, 9, 2, 5, 1});
        System.out.println(maxUncrossedLines);
    }

    public int maxUncrossedLines(int[] nums1, int[] nums2) {
        int[][] dp = new int[nums1.length + 1][nums2.length + 1];
        for (int i = 1; i <= nums1.length; i++) {
            for (int j = 1; j <= nums2.length; j++) {
                if (nums1[i - 1] == nums2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                }
                int max = Math.max(dp[i - 1][j], dp[i][j - 1]);
                dp[i][j] = Math.max(max, dp[i][j]);
            }
        }
        return dp[nums1.length][nums2.length];
    }
}
